package com.amumu.drama.common.config;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author sunzhuang
 * @date 2024/5/13 15:06
 * @slogan 天下风云出我辈, 一入代码岁月催
 * @desc redis 分布式锁配置
 */

@Data
// @NoArgsConstructor
// @AllArgsConstructor
// @Builder(toBuilder = true)
@Component
@ConfigurationProperties(prefix = "opentide.redisson.lock")
public class RedissonLockProperties {

    /**
     * 等待获取锁的时间
     */
    private int waitTime;

    /**
     * 释放锁的时间
     */
    private int leaseTime;

    /**
     * 是否启用
     */
    private Boolean enable = false;

    /**
     * 业务模块名称,请根据业务内容自定义; eg：order
     */
    private String lockKeyPrefix;

    /**
     * 时间单位：minutes(分钟)、seconds(秒)、milliseconds(毫秒)、microseconds(毫秒的千分之一)
     */
    private String timeUnitStr;

    // private TimeUnit timeUnit;

    public TimeUnit getTimeUnit() {
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        String unitStr = getTimeUnitStr();
        if (StringUtils.isNotEmpty(unitStr)) {
            switch (unitStr) {
                case "minutes":
                    timeUnit = TimeUnit.MINUTES;
                    break;
                case "seconds":
                    timeUnit = TimeUnit.SECONDS;
                    break;
                case "milliseconds":
                    timeUnit = TimeUnit.MILLISECONDS;
                    break;
                case "microseconds":
                    timeUnit = TimeUnit.MICROSECONDS;
                    break;
                default:
                    break;
            }
        }
        return timeUnit;
    }

}